const express = require('express')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
const passport = require('passport')

const user = require('./routers/api/user')
const todo = require('./routers/api/todo')

const {
  port,
  mongodbURI
} = require('./config/key')

const app = express()

// 配置body-parser
app.use(bodyParser.urlencoded({
  extended: false
}))
app.use(bodyParser.json())

// passport init
app.use(passport.initialize())
require('./config/passport')(passport)

// 连接mongodb数据库
mongoose.connect(mongodbURI, {
    useNewUrlParser: true
  })
  .then(() => {
    console.log('mongodb is connect')
  })
  .catch((error) => {
    console.log(error)
  })

// 配置用户路由
app.use('/api/user', user)
app.use('/api/todo', todo)

// 监听端口
app.listen(port, () => {
  console.log(`server is runnig in port ${port}`)
})